// src/composables/useProductForm.ts
import { ref } from 'vue'
import type { Ref } from 'vue'
import type { FormInstance } from 'element-plus'
import * as Api from '@/api/index'

export function useProductForm() {
  const ruleFormRef = ref<FormInstance>()
  const dialogSpecVisible = ref(false)
  
  // 表单验证规则
  const rules = {
    name: [
      { required: true, message: '请输入商品名称', trigger: 'blur' },
      { min: 2, max: 30, message: '请输入2-30位商品名称', trigger: 'blur' }
    ],
    price: [
      { required: true, message: '请输入商品价格', trigger: 'blur' },
      { 
        validator: (_:any, value: number) => {
          if (value <= 0) return Promise.reject('商品价格必须大于0')
          if (!/^\d+(\.\d{1,2})?$/.test(value.toString())) return Promise.reject('金额最多保留两位小数')
          if (value > 1000000) return Promise.reject('金额不能超过1,000,000')
          return Promise.resolve()
        },
        trigger: ['blur', 'change']
      }
    ]
  }

  // 获取商品详情的SKU信息
const fetchProductSkuInfo = async (id: string) => {
  if (!id) return null;
  
  try {
    const res = await Api.getProductDetailSkus(id, { isAdmin: 1 });
    console.log('获取商品详情的SKU信息:', res);
    return res.data?.skus || res.data || [];
  } catch (error) {
    console.error('获取SKU失败:', error);
    return null;
  }
}

  return {
    ruleFormRef,
    dialogSpecVisible,
    rules,
    fetchProductSkuInfo
  }
}